package com.feditory.view.category;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.feditory.category.domain.Category;
import com.feditory.category.domain.CategoryChannelType;
import com.feditory.category.domain.CategoryType;
import com.feditory.category.provider.CategoryProvider;
import com.feditory.view.category.service.CategoryControllerService;
import com.feditory.view.category.vo.MainCategoryListVO;
import com.feditory.view.category.vo.MainViewVO;

@Controller
public class CategoryController implements CategoryControllerService {

    @Autowired
    private CategoryProvider categoryProvider;

    public void setCategoryProvider(CategoryProvider categoryProvider) {
        this.categoryProvider = categoryProvider;
    }

    //    @RequestMapping(method = RequestMethod.POST,  value = "/category", headers = "Accept=application/xml, application/json")
    //    public @ResponseBody
    //    CategoryRegisterVO registerCategory(@RequestBody
    //    CategoryRegisterVO categoryVo) {
    //        Category category = new Category();
    //        category.setCategoryName("sdfsdfsdf");
    //        
    ////        HttpHeaders headers = new HttpHeaders();
    //////        MediaType mediaType = new MediaType("text", "html", Charset.forName("utf-8"));
    //////        headers.setContentType(mediaType);
    ////        headers.add("content-type", "application/xml; charset=utf-8");
    //        
    ////        this.categoryProvider.createCategory(category);
    //        List<CategoryRegisterVO> categoryVos = new ArrayList<CategoryRegisterVO>();
    //        return new CategoryRegisterVO();
    //    }

    //    @RequestMapping(value = "/category/{id}", method = RequestMethod.GET, headers = "Accept=application/xml, application/json")
    //    public @ResponseBody
    //    CategoryRegisterVO getCategory(@PathVariable("id")
    //    Long id) {
    //        CategoryRegisterVO cc = new CategoryRegisterVO();
    //        return cc;
    //    }

    @Override
    public MainViewVO findAllMainCategories() {
        // Main 카테고리 조회
        List<Category> categories = this.categoryProvider.retrieveMainCategoryAllList(CategoryChannelType.SMART_DOME, CategoryType.MAIN);
        if (categories == null || categories.isEmpty()) return null;

        MainViewVO view = new MainViewVO();
        List<MainCategoryListVO> categoryVos = new ArrayList<MainCategoryListVO>();
        for (Category category : categories) {
            MainCategoryListVO mainCategory = new MainCategoryListVO();
            mainCategory.setCategoryId(category.getCategoryId());
            mainCategory.setCategoryName(category.getCategoryName());
            mainCategory.setLoginUseableYn(category.isLoginUseableYn());
            mainCategory.setDisplayOrder(category.getDisplayOrder());

            List<Category> subCategoryList = this.categoryProvider.retrieveCategoryListByParentId(category.getCategoryId());
            if (subCategoryList != null && !subCategoryList.isEmpty()) {
                for (Category subCategory : subCategoryList) {
                    MainCategoryListVO subCategoryVo = new MainCategoryListVO();
                    subCategoryVo.setCategoryId(subCategory.getCategoryId());
                    subCategoryVo.setCategoryName(subCategory.getCategoryName());
                    subCategoryVo.setLoginUseableYn(subCategory.isLoginUseableYn());
                    subCategoryVo.setDisplayOrder(subCategory.getDisplayOrder());

                    // 하위 카테고리추가
                    mainCategory.addSubCategory(subCategoryVo);
                }
            }
            categoryVos.add(mainCategory);
        }
        view.setMainList(categoryVos);
        return view;

    }
}
